package com.sxt.business.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxt.business.domain.Customer;
import com.sxt.business.domain.Goods;
import com.sxt.business.domain.Sales;
import com.sxt.business.domain.Salesback;
import com.sxt.business.mapper.CustomerMapper;
import com.sxt.business.mapper.SalesMapper;
import com.sxt.business.mapper.SalesbackMapper;
import com.sxt.business.service.IGoodsService;
import com.sxt.business.service.ISalesbackService;
import com.sxt.business.vo.SalesbackVO;
import com.sxt.system.utils.DataGridView;
import com.sxt.system.utils.WebAppUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 俊哥
 * @since 2019-11-16
 */
@Service
public class SalesbackServiceImpl extends ServiceImpl<SalesbackMapper, Salesback> implements ISalesbackService {

	@Autowired
	private SalesMapper salesMapper;
	
	@Autowired
	private CustomerMapper customerMapper;
	
	@Override
	public void addSalesback(Salesback salesback) {
		Integer salesid = salesback.getSalesid();
		//根据进货单ID查询进货信息
		Sales sales = salesMapper.selectById(salesid);
		//向salesback里卖弄封装数据
		salesback.setGoodsid(sales.getGoodsid());
		salesback.setSalebackprice(sales.getSalesprice());
		salesback.setPaytype(sales.getPaytype());
		//保存退货数据
		this.getBaseMapper().insert(salesback);
	
		//更新销售数据
		sales.setNumber(sales.getNumber()-salesback.getNumber());
		this.salesMapper.updateById(sales);
		
		//更新库存
		IGoodsService iGoodsService = WebAppUtils.getContent().getBean(IGoodsService.class);
		Goods goods = iGoodsService.getById(sales.getGoodsid());
		goods.setNumber(goods.getNumber()+salesback.getNumber());
		iGoodsService.updateById(goods);
	}

	@Override
	public Integer loadSalesbackBySalesId(Integer salesId) {
		return  this.getBaseMapper().querySalesbackCountBySalesId(salesId);
	}

	@Override
	public DataGridView queryAllSalesback(SalesbackVO salesbackVO) {
		Page<Salesback> page = new Page<Salesback>(salesbackVO.getPage(), salesbackVO.getLimit());
		
		QueryWrapper<Salesback> qw = new QueryWrapper<Salesback>();
		qw.eq(salesbackVO.getCustomerid()!=null, "customerid", salesbackVO.getCustomerid());
		qw.eq(salesbackVO.getGoodsid()!=null, "goodsid", salesbackVO.getGoodsid());
		qw.ge(null!=salesbackVO.getStartTime(), "salesbacktime", salesbackVO.getStartTime());
		qw.le(null!=salesbackVO.getEndTime(), "salesbacktime", salesbackVO.getEndTime());
		qw.orderByDesc("salesbacktime");
		this.getBaseMapper().selectPage(page, qw);
		List<Salesback> list = page.getRecords();
		for (Salesback salesback : list) {
			Integer customerid = salesback.getCustomerid();
			Integer goodsid = salesback.getGoodsid();
			if(null!=customerid) {
				Customer customer = customerMapper.selectById(customerid);
				salesback.setCustomername(customer.getCustomername());
			}
			if(null!=goodsid) {
				Goods goods = WebAppUtils.getContent().getBean(IGoodsService.class).getById(goodsid);
				salesback.setGoodsname(goods.getGoodsname());
				salesback.setSize(goods.getSize());
			}
		}
		return new DataGridView(page.getTotal(), list);
	}

}
